<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login_model extends CI_Model{
	var $CI;
	function __construct($table=''){
  		parent::__construct();
  		$this->CI =& get_instance();
  		$this->CI->load->model('Data_model');
		$this->CI->load->library ('mysession');
		$this->CI->load->helper ('myfunc');
	}
	
	function login($username,$userpass, $role){
		log_message(ETS_INFO, '### M:Login_model->login');
		$this->CI->Data_model->setTable(TB_USER);
		$user = $this->CI->Data_model->getSingle(array(TB_USER_NAME=>$username));
		$isUserOK = isset($user[TB_USER_STATUS])
				&& $user[TB_USER_STATUS] == TB_USER_STATUS_ACTIVE
				&& $user[TB_USER_ROLE] == $role
				&& $user[TB_USER_PASSWORD]==md5pass($userpass,$username);
		log_message(ETS_INFO, 'pwd correct:'.($user[TB_USER_PASSWORD] == md5pass($userpass,$username)));
		if($isUserOK){
			$this->CI->Data_model->editData(
					array(TB_USER_ID=>$user[TB_USER_ID]),
					array(TB_USER_LOGINCOUNT=>$user[TB_USER_LOGINCOUNT]+1,TB_USER_LASTTIME=>time()));
			$this->CI->mysession->login(
					$user[TB_USER_ID],
					$user[TB_USER_NAME],
					$user[TB_USER_ROLE]);
		}
		log_message(ETS_INFO, '### m:login return '.$isUserOK);
		return $isUserOK;
	}
}